// Created, updated and maintained by Taufik Nurrohman.
// URL: https://plus.google.com/108949996304093815163/about

var $window = $(window),
	$slideshow = $('#slideshow'),
	$slideinner = $slideshow.find('.slideshow-inner'),
	$slidenav = $slideshow.find('.slideshow-nav');
function mySlideShow(json) {
	var a = json.feed.entry, link, thumb, thumbsmall, title, summ, sk = "", sn = "";
	sn = '<div class="slideshow-nav-inner"><div>';
	for (var i = 0, len = a.length; i < len; i++) {
		for (var j = 0, llh = a[i].link.length; j < llh; j++) {
			link = (a[i].link[j].rel=="alternate") ? a[i].link[j].href : "#nope";
		}
		title = a[i].title.$t;
		thumb = ("media$thumbnail" in a[i]) ? '<img class="image left-img" src="'+a[i].media$thumbnail.url.replace("/s72-c","/s200-c")+'" alt="">' : '<span class="image left-img"></span>';
		thumbsmall = ("media$thumbnail" in a[i]) ? '<img class="image pull-left" src="'+a[i].media$thumbnail.url+'" alt="Loading..." title="'+title+'">' : '<span class="image pull-left" title="'+title+'"></span>';
		summ = ("content" in a[i]) ? a[i].content.$t.replace(/<img(.*?)>/g,"") : ("summary" in a[i]) ? a[i].summary.$t : '...'
		sk += '<div class="slide-item pull-left"><a href="' + link + '">' + thumb + '</a><h6>' + title + '</h6><div class="p">' + summ + '</div><p class="text-right"><a class="btn" href="' + link + '">Belanja Sekarang</a></p></div>';
		sn += thumbsmall;
	}
	sn += '</div></div><span class="left-nav">&laquo;</span><span class="right-nav">&raquo;</span>';
	$slideinner.html($slideinner.html()+sk);
	$slidenav.html(sn);
	$window.on("resize", function() {
		var w = $slideshow.width();
		$slideinner.css({'width':w*(a.length+1),'left':0}).find('.slide-item').css('width',w).find('.product-description').each(function() {
			var str = $(this).text();
			$(this).text(str.substring(0,250)+'...');
		});
	}).trigger("resize");
	$slidenav.find('.image').on("click", function() {
		var index = $(this).index()+1,
			distance = -$slideshow.width()*index;
		$(this).parent().find('.current').removeClass('curent');
		$(this).addClass('current');
		$slideinner.stop().animate({left:distance}, 1000);
	}).first().addClass('current').parent().css('width',82*a.length);
	$slidenav.find('.left-nav').on("click", function() {
		$slidenav.find('.slideshow-nav-inner').stop().animate({scrollLeft:"-=100px"}, "slow");
	}).next().on("click", function() {
		$slidenav.find('.slideshow-nav-inner').stop().animate({scrollLeft:"+=100px"}, "slow");
	});
}

(function() {
	var h = document.getElementsByTagName('head')[0],
		s = document.createElement('script');
		s.type = "text/javascript";
		s.src = homepage+'feeds/posts/default?alt=json-in-script&redirect=false&max-results=20&callback=mySlideShow';
	h.appendChild(s);
})();